<template>
  <svg v-if="isCustomIcon" :class="svgClass" aria-hidden="true">
    <use :xlink:href="iconName"></use>
  </svg>
  <el-icon v-else :class="svgClass">
    <component :is="iconComponent"></component>
  </el-icon>
</template>

<script setup>
  import { computed } from 'vue';
  import * as ElementPlusIcons from '@element-plus/icons-vue';

  const props = defineProps({
    name: {
      type: String,
      required: true,
    },
    className: {
      type: String,
      default: '',
    },
    type: {
      type: String,
      default: 'svg', // 'svg' or 'el'
    },
  });

  const isCustomIcon = computed(() => props.type === 'svg');

  const iconName = computed(() => (isCustomIcon.value ? `#icon-${props.name}` : null));

  const iconComponent = computed(() => {
    if (!isCustomIcon.value) {
      const iconComponentName = props.name.replace(/^\w/, (c) => c.toUpperCase());
      return ElementPlusIcons[iconComponentName] || null;
    }
    return null;
  });

  const svgClass = computed(() => ({
    'svg-icon': true,
    [props.className]: !!props.className,
  }));
</script>

<style scoped>
  .svg-icon {
    width: 1em;
    height: 1em;
    vertical-align: -0.15em;
    fill: currentColor;
    overflow: hidden;
  }
</style>
